package com.thy.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.base.PageResult;
import com.hospital.common.base.ThreadAttributes;
import com.hospital.common.dto.audit.module.DeletePostDto;
import com.hospital.common.dto.audit.module.PostReviewDto;
import com.hospital.common.pojo.TPostMeta;
import com.hospital.common.utils.ThreadLocalUtils;
import com.hospital.common.vo.audit.module.PostVo;
import com.thy.aop.LogAction;
import com.thy.mapper.TPostMetaMapper;
import com.thy.service.TPostMetaService;
import com.thy.utils.AuitLogUtil;
import com.thy.utils.MessageUtil;
import com.thy.utils.SensitiveWordChecker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 帖子元数据表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-26
 */
@Service
public class TPostMetaServiceImpl extends ServiceImpl<TPostMetaMapper, TPostMeta> implements TPostMetaService {
    @Autowired
    private TPostMetaMapper tPostMetaMapper;
    @Override
    public Integer getViolation(String content) {
        boolean check = SensitiveWordChecker.check(content);
        if(!check){
            return 200;
        }
        return -1;
    }

    @Override
    public PageResult<PostVo> getPost(Integer pageNum, Integer pageSize, Long userId) {
        //根据userId查询板块id
        Long sectionId = tPostMetaMapper.getSectionId(userId);
        Page<PostVo> page = new Page<>(pageNum,pageSize);
        IPage<PostVo> postVos = tPostMetaMapper.getPost(page,sectionId);
            return new PageResult<>(postVos.getTotal(), postVos.getRecords());
    }

    @Override
    public void postReview(PostReviewDto postReviewDto) {
        ThreadAttributes threadAttributes = ThreadLocalUtils.get();
        Long userId = threadAttributes.getUserId();
        if (postReviewDto.getReviewStatus() == -1){
            //拒绝
            tPostMetaMapper.postReviewRefuse(postReviewDto,userId);
            //通知用户发帖失败
            MessageUtil.sendMessage(userId,postReviewDto.getAuthorId(),"您的发帖审核已驳回！驳回理由："+postReviewDto.getReviewReason());
            //添加日志
            AuitLogUtil.log(userId, LogAction.POST_REVIEW_REJECTED.getId(), postReviewDto.getAuthorId(), AuitLogUtil.getRole(postReviewDto.getAuthorId()).longValue());
        }else if (postReviewDto.getReviewStatus() == 1){
            //通过
            tPostMetaMapper.postReviewThrough(postReviewDto.getMetaId());
            //通知用户发帖成功
            MessageUtil.sendMessage(userId,postReviewDto.getAuthorId(),"您的帖子审核已通过！");
            //添加日志
            AuitLogUtil.log(userId, LogAction.POST_APPROVED.getId(), postReviewDto.getAuthorId(), AuitLogUtil.getRole(postReviewDto.getAuthorId()).longValue());
        }
    }

    @Override
    public void deletePost(DeletePostDto dpd) {
        ThreadAttributes threadAttributes = ThreadLocalUtils.get();
        Long userId = threadAttributes.getUserId();
        tPostMetaMapper.deletePost(dpd,userId);
        AuitLogUtil.log(userId, LogAction.POST_DELETED.getId(), dpd.getAuthorId(), AuitLogUtil.getRole(dpd.getAuthorId()).longValue());
        if (dpd.getAuthorId().longValue() != userId.longValue()){
            MessageUtil.sendMessage(userId,dpd.getAuthorId(),"您的帖子包含违规信息,已被管理员删除！");
        }
    }
}
